const http = require("http");
const application = http.createServer();
const url = require("url");
// 客户端发送请求
// application.on("req", (request, response) => {
// 第一个参数request是固定的写法
application.on("request", (request, response) => {
  // 请求方式
  console.log(request.method);
  // console.log(request.headers.accept);
  // console.log(request.headers['accept']);
  // 服务器响应。结束这一次的请求
  // console.log(url.parse(request.url,true));
  console.log(request.url,'url');
  let {pathname,query}=url.parse(request.url);
  console.log(pathname,query);
  if (pathname == "/index" || pathname === "/") {
    // 写请求头信息
    response.writeHead(200, {
      // 写utf8,或者utf-8
      "content-type": "text/html;charset=utf-8",
    });
    response.end("<h1>欢迎来到首页</h1>");
    return;
  }
  if(pathname=='/list'){
    response.writeHead(200,{
      "content-type": "text/html;charset=utf-8",
    })
    response.end("<h1>欢迎来到LIST</h1>");
    return;
  }
  response.end("<h1>sorry,this page is not found!</h1>");
});
// 监听一个端口号
application.listen(3006,(err=>{
  if(err){
    console.log('服务器启动失败');
    return
  }
  console.log('请访问 http://localhost:3006');
}));

